package de.upb.aquarium.api.light;

import java.util.Arrays;

import javax.media.opengl.GL;

import de.upb.aquarium.api.utility.VectorFloat;

public class PointLightSource extends Light {

	private VectorFloat position;
	
	private float[] attenuation;
	
	/**
	 * Creates  a new PointLightSource with the given three colors at the
	 * given position.
	 * @param position The direction of the light.
	 * @param colorAmbient The ambient color.
	 * @param colorDiffuse The diffuse color.
	 * @param colorSpecular The specular color.
	 */
	public PointLightSource(
			VectorFloat position,
			float attenuationConst,
			float attenuationLinear,
			float attenuationQuadratic,
			VectorFloat colorAmbient,
			VectorFloat colorDiffuse,
			VectorFloat colorSpecular)
	{
		super (colorAmbient, colorDiffuse, colorSpecular);
		
		setPosition(position);
		
		attenuation = new float[3];
		attenuation[0] = attenuationConst;
		attenuation[1] = attenuationLinear;
		attenuation[2] = attenuationQuadratic;
	}

	public VectorFloat getPosition() {
		return position;
	}

	@Override
	public void enable(GL gl, int lightIdentifier) {
		float[] pos = Arrays.copyOf(position.data, 4);
		
		pos[3] = 1.0f;
		
		gl.glLightfv(lightIdentifier, GL.GL_POSITION, pos, 0);
	}	
	
	@Override
	public void disable() {
		
	}

	public void setPosition(VectorFloat position) {
		
		if (position.getSize() != 3)
		{
			throw new IllegalArgumentException("Position of a light source " +
					"must have exactly 3 coordinates (not homogenous).");
		}
		this.position = position;
	}
}
